<?php
namespace app\common\service;



use app\common\helper\WeixinHelper;
use app\common\util\Util;

class ResourceDownloadService extends BaseService
{

    /**
     * 后台: 获取资源下载认证二维码
     * @param $sid
     * @return array
     */
    public function getDownloadAuthQRcode($sid){
        $token = Util::createResourceDownloadToken($sid);
        $operator_id = db('user_integral')->where(['sid' => $sid, 'is_administrator' => 1])->value('userid');
        if(!$operator_id){
            return ['status' => 0, 'msg' => '请先设置管理员'];
        }

        $data = [
            'token' => $token,
            'operator_id' => $operator_id
        ];

        $r = db('resource_download_auth')->insert($data);
        if(!$r){
            return ['status' => 0, 'msg' => '系统出错'];
        }

        $save_path = ROOT_PATH .'public'.DS.'uploads'.DS .'resource_download' . DS . $sid . DS;
        $url = $this->domain.'/m/html/confirm?sid='.$sid.'&token='.$token;
        $qrcode = Util::createQRcode($save_path, $url, 'L', '4', 'qrcode', true);
        $qrcode_info = pathinfo($qrcode);


//        $qrcode = WeixinHelper::get_temporary_qrcode($sid, 'resource_download_'.$token);

        if(!$qrcode){
            return ['status' => 0, 'msg' => '生成二维码失败'];
        } else {
            $qrcode_url = $this->domain.'/uploads/resource_download/'.$sid.DS.$qrcode_info['basename'];
            $data = [
                'qrcode_url' => $qrcode_url,
                'token' => $token
            ];
            return ['status' => 1, 'data' => $data, 'msg' => '请求成功'];
        }
    }


    public function tokenAuth($token){
        $info = db('resource_download_auth')->where(['token' => $token])->find();
        if(empty($info)){
            return ['status' => 0, 'msg' => '获取信息失败'];
        }

        $auth_status = 0;
        if($info['operate_status'] == 1){
            if($info['token_status'] == 1){
                $auth_status = 1;
                //token成功验证一次则失效
                db('resource_download_auth')->where(['token' => $token])->setField('token_status', 0);
            } else {
                //token已验证一次以上，失效状态
                $auth_status = 2;
            }
        }
        return ['status' => 1, 'msg' => '请求成功', 'data' => ['auth_status' => $auth_status]];
    }

    /**
     * 接口token校验
     * @param $token
     * @return bool
     */
    public static function apiTokenValidate($token){
        $info = db('resource_download_auth')->where(['token' => $token, 'operate_status' => 1, 'token_status' => 0])->find();
        if($info && $info['api_status'] == 0){
            db('resource_download_auth')->where(['token' => $token])->setField('api_status', 1);
            return true;
        } else {
            return false;
        }
    }

    /**
     * 添加下载操作日志
     * @param $sid
     * @param $log
     * @return int|string
     */
    public static function addDownloadLog($sid, $log){
        $operator_id = db('user_integral')->where(['sid' => $sid, 'is_administrator' => 1])->value('userid');
        $data = [
            'sid' => $sid,
            'log' => $log,
            'operator_id' => $operator_id,
            'operate_time' => time()
        ];
        return db('resource_download_log')->insert($data);
    }


    /**
     * 获取下载操作日志记录
     * @param $sid
     * @param $p
     * @param $pageSize
     * @return array
     */
    public function getDownloadLogList($sid, $p, $pageSize){
        $list = db('resource_download_log r')
                ->field('u.user_nickname,r.*')
                ->join('user u', 'u.user_id=r.operator_id')
                ->where(['r.sid' => $sid])
                ->limit(($p-1)*$pageSize, $pageSize)
                ->order('r.id DESC')
                ->select();
        if($list){
            foreach($list as $k => $v){
                $list[$k]['operate_time'] = date('Y-m-d H:i:s', $v['operate_time']);
                $list[$k]['log_desc'] = '管理员('.$v['user_nickname'].'),'.$v['log'];
            }
        }
        $count = db('resource_download_log r')
                ->join('user u', 'u.user_id=r.operator_id')
                ->where(['r.sid' => $sid])
                ->count();
        $data = [
            'page' => ['count' => $count],
            'content' => $list
        ];
        return $data;
    }
}